//结算单详情

interface IFeeSettleDetailViewModel extends IBaseViewModel<IFeeSettleDetailQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<FinanceFeeListItemResponse>;
    /**
     * 禁用/显示 结算单详情导出
     */
    canExcel:boolean;
    /**
     * 应付结算总单详情数据
     */
    feeData:FeeSettleBillDetailResponse;
    /**
     * 银行卡是否修改
     */
    cardIsChange:boolean;
    /**
     * 是否单次结算
     */
    isOneFee:boolean;
    /**
     * 费用状态
     */
    sFeeStatusList:string;
    /**
     * 费用对账
     */
    checkDetail:(id:string)=>void;
    /**
     * 承运单编号
     */
    carrierOrderId:string;
    /**
     * 返回
     */
    back:()=>void;
    // /**
    //  * 付款流水
    //  */
    // feePayList:()=>void;
    /**
     * 付款记录
     */
    payLogList:Array<paymentLogListItemResponse>;
    /**
     * 导出列表
     */
    excel:()=>void;   
    /**
     * 列表高
     */ 
    heightA:number;
}

/**
 * 查询条件
 */
interface IFeeSettleDetailQueryTerms{
    /**
     * 费用编号
     */
    feeCode:string;
    /**
     * 合同号
     */
    contractNumber:string;
    /**
     * 结算承运商
     */
    carrier:string;
    /**
     * 车牌号
     */
    carCode:string
}

class IFeeSettleDetailController extends ListController<IFeeSettleDetailViewModel,IFeeSettleDetailQueryTerms>{
    gridApi: uiGrid.IGridApiOf<FinanceFeeListItemResponse>;

    asc:boolean = true;
    orderBy:string = "";
    //银行卡是否修改
    cardIsChange = false;
    //是否单次结算
    isOneFee = false;
    //费用状态
    sFeeStatusList ="0,1,2,3,4,5,6,7,8,9,10,11";
    //可识别结算总单编号
    billCode:string = "";
    //禁用 结算单详情导出
    canExcel = false;
    /**
     * 名称
     */
    name:string;
     /**
     * 结算编号
     */
    settleId:string;

    constructor($scope:IFeeSettleDetailViewModel, private feeService:IFeeService, private $location:ng.ILocationService, 
    private authorizationService:IAuthorizationService, private orderService: IorderService, private $state: angular.ui.IStateService,
    private exportService:IexportService,private $ngBootbox: BootboxService){
        super($scope);
        this.init();
    }

    init():void{
        super.init();

        this.settleId = this.$location.search().settleId;
        this.name = this.$location.search().name;

        this.$scope.queryTerms = {
            feeCode:"",
            contractNumber:"",
            carrier:"",
            carCode:""
        }

        this.$scope.gridOptions = {
            exporterMenuCsv: false,
			exporterMenuPdf: false,
			enableGridMenu: true,
			enableSelectAll: false,//全选禁用
			paginationPageSizes: [10, 20, 30],
			useExternalPagination: true,
			useExternalSorting: true,
			enableColumnResizing: true,
            columnDefs: [
                { displayName: "应付编号", field: 'feeCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: "合同号", field: 'contractNumber', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '结算承运商', field: 'carrier', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: "承运商号码", field: 'carrierPhone', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                { displayName: '车牌号', field: 'carCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '应付单价', field: 'payablePriceStr', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '应付说明', field: 'expenditureReasons', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '付款方式', field: 'paymentMethod', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '费用类型', field: 'feeType', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '费用金额', field: 'feeAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '费用状态', field: 'feeStatus', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '申领人员', field: 'applicant', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '申请日期', field: 'creationTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '结算日期', field: 'settleTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '结算审核人', field: 'auditedUserName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '结算审核日期', field: 'auditedTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '付款日期', field: 'payTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" title="费用对账" class="fa fa-eye m-l-xs  primary text-info" ng-click="grid.appScope.checkDetail(row.entity.id)"></a>', enableColumnMenu: false
                }
            ],
            onRegisterApi:(gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            } 
        };
  
        
        //结算单详情导出权限
        this.isAuth();
        //this.query();
        this.getDetail();
        this.$scope.checkDetail = this.checkDetail;
        this.$scope.back = this.back;
        this.feePayList();

    }

    /**
     * 加载数据
     */
    loadData(queryTerms:IFeeSettleDetailQueryTerms,skip:number,count:number):void{
        this.feeService.getFinanceFeeList(this.orderBy,this.asc, this.billCode ,this.queryTerms.feeCode,"","","",this.queryTerms.contractNumber,"","","","","","","","","","","","",
        this.queryTerms.carrier,"",this.queryTerms.carCode,"",this.sFeeStatusList,"","","","",skip,count).then((res)=>{
            this.$scope.gridOptions.totalItems = res.data.total;
                this.$scope.gridOptions.data = res.data.data;
                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39)+96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
         });  
    }

    getListCount():void{
        this.feeService.getFinanceFeeCount(this.billCode ,this.$scope.queryTerms.feeCode,"","","",this.$scope.queryTerms.contractNumber,"","","","","","","","","","","","",
        this.$scope.queryTerms.carrier,"",this.$scope.queryTerms.carCode,"",this.sFeeStatusList,"","","","").then(res=>{
            this.$scope.gridOptions.totalItems = res.data.extData;
        })
    }



    /**
     * 应付结算总单详情数据
     */
    getDetail=()=>{
        this.feeService.getFeeSettleBillDetail(this.settleId).then((res)=>{
            this.$scope.feeData = res.data;
            this.billCode = res.data.billCode;
            if(res.data.isChangeBank == true ){
                this.cardIsChange = true;
            }
            if(res.data.isFeeSingle == 'true' ){
                this.isOneFee = true;
            }
            this.query();
        });
    }

    /**
     * 结算单详情导出权限
     */
    isAuth=()=>{
        this.authorizationService.auth('24E0A026891BC3903C7917417F680C9D0111FCF336FB58E79A88A2D483574277').then((res)=>{
            this.$scope.canExcel = res.data.extData;
        });
    }

    /**
     * 费用对账
     */
    checkDetail=(id:string)=>{
        this.feeService.feeDetail(id).then((res)=>{
            this.$scope.carrierOrderId = res.data.carrierOrderId;
            this.orderService.carryOrder(this.$scope.carrierOrderId).then((result)=>{
                if(result.data.orderList.length > 1){
                    if(this.name == 'settle'){
                        this.$state.go('app.fee.feeBreakbulkDetail',{ feeId:id, carrierOrderId:this.$scope.carrierOrderId, pageName:'settleManage' });
                    }
                    if(this.name=='settlepay'){
                         this.$state.go("app.fee.feeBreakbulkDetail", { feeId: id, carrierOrderId: this.$scope.carrierOrderId, pageName: 'settlepay' });
                    }
                }else{
                    if(this.name=='settle'){
                        this.$state.go('app.fee.feeOrderDetailController',{ feeId:id, orderId:result.data.orderList[0], carrierOrderId:this.$scope.carrierOrderId, name:'SettleManageDetail' });
                    }
                    if(this.name == 'settlepay'){
                        this.$state.go("app.fee.feeOrderDetailController", { feeId: id, orderId: result.data.orderList[0], carrierOrderId: this.$scope.carrierOrderId, name: 'SettlePayManageDetail' });
                    }
                }
            });
        });
    }

    /**
     * 返回
     */
    back=()=>{
        if(this.name == 'settle'){
            this.$state.go('app.fee.feeSettle_admin',{name:'feeSettle_admin'});
        }
        if(this.name == 'settlepay'){
            this.$state.go("app.fee.feeSettlePayManage",{name:'feeSettlePayManage'})
        }
    }

    /**
     * 付款流水
     */
    feePayList=()=>{
        this.feeService.paymentLogList("",null,this.settleId,"","","",0,-1).then((res)=>{
            this.$scope.payLogList = res.data.data;
        });
    }

    /**
     * 导出
     */
    exportFunc=()=>{
        if( this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.detailexport(this.billCode,this.queryTerms.feeCode,this.queryTerms.carrier,this.queryTerms.carCode,this.queryTerms.contractNumber,this.sFeeStatusList);
        }
    }
    
}
angular.module("tms").controller("feeSettleDetailController", ["$scope", "feeService", "$location", "authorizationService" , "orderService", "$state","exportService","$ngBootbox",  IFeeSettleDetailController]);
